package com.hkfrich.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkfrich.common.utils.SecurityUtils;
import com.hkfrich.common.utils.StringUtils;
import com.hkfrich.generator.utils.DictUtils;
import com.hkfrich.system.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import com.hkfrich.common.controller.BaseController;
import com.hkfrich.common.utils.ConvertUtils;
import com.hkfrich.common.utils.ResultUtils;
import com.hkfrich.system.domain.SysDictData;
import com.hkfrich.system.service.SysDictDataService;
//import com.hkfrich.common.core.page.TableDataInfo;

/**
 * @ClassName 字典数据Controller
 * @Desscription
 * @Author _Heishuai
 * @Date 2022-09-15 00:08:52
 * @Version 1.0
 */
@Controller
@RequestMapping("/system/dictdata")
public class SysDictDataController extends BaseController {

    @Autowired
    private SysDictDataService sysDictDataService;
    @Autowired
    private SysDictTypeService sysDictTypeService;
    /**
     * 根据字典类型查询字典数据信息
     */
    @RequestMapping("/getDicts")
    @ResponseBody
    public ResultUtils dictType(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        String dictType = params.getString("dictType");
        List<SysDictData> data = sysDictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return ResultUtils.success(data,"字典数据获取成功");
    }



    /**
     * 通过条件查询单个字典数据 不分页
     */
    @RequestMapping("/getOne")
    @ResponseBody
    public ResultUtils getOne(SysDictData sysDictData)
    {
        QueryWrapper<SysDictData> query = initBaseQueryWrapper(sysDictData);
        SysDictData one = sysDictDataService.getOne(query);
        return ResultUtils.success(one,"字典数据查询成功");
    }

    /**
     * 通过id查询单个字典数据
     */
    @RequestMapping("/getOneById")
    @ResponseBody
    public ResultUtils getOneById(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Integer id = params.getInteger("id");
        SysDictData one = sysDictDataService.getById(id);
        return ResultUtils.success(one,"字典数据查询成功");
    }

    /**
     * 查询字典数据列表  分页
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultUtils list(@RequestBody SysDictData sysDictData)
    {
        Page<SysDictData> page = initBaseQueryPageWrapper(sysDictData);
        QueryWrapper<SysDictData> query = initBaseQueryWrapper(sysDictData);
        Page<SysDictData> pageGrid = sysDictDataService.page(page, query);
        return ResultUtils.success(pageGrid,"字典列表查询成功");
    }

    /**
     * 新增字典数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultUtils save(@RequestBody SysDictData sysDictData)
    {
        sysDictData.setCreateBy(SecurityUtils.getUserId());
        boolean isok = sysDictDataService.save(sysDictData);
        if (isok)
        {
            List<SysDictData> dictDatas = sysDictDataService.selectDictDataByType(sysDictData.getDictType());
            DictUtils.setDictCache(sysDictData.getDictType(), dictDatas);
        }
        return ResultUtils.success("字典数据保存成功");
    }

    /**
     * 修改字典数据
     */
    @RequestMapping("/updateById")
    @ResponseBody
    public ResultUtils updateById(@RequestBody SysDictData sysDictData)
    {
        sysDictData.setUpdateBy(SecurityUtils.getUserId());
        sysDictDataService.updateDictData(sysDictData);
        return ResultUtils.success("字典数据修改成功");
    }

    /**
     * 删除字典数据
     */
    @RequestMapping("/removeById")
    @ResponseBody
    public ResultUtils removeById(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Integer id = params.getInteger("id");
        return ResultUtils.success(sysDictDataService.removeById(id),"字典数据删除成功");
    }

    /**
     * 批量删除字典数据
     */
    @RequestMapping("/removeByIds")
    @ResponseBody
    public ResultUtils removeByIds(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        String ids = params.getString("ids");
        List<Long> list = ConvertUtils.toList(ids);
        sysDictDataService.deleteDictDataByIds(list);
        return ResultUtils.success("批量删除字典数据成功");
    }

}
